package com.sl.biz.modular.process.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.sl.auth.core.util.StpCompositeUtil;
import com.sl.biz.modular.process.controller.request.ProcessCompObjectRebuildRequest;
import com.sl.biz.modular.process.dao.domain.entity.ProcessCompObjectDO;
import com.sl.biz.modular.process.service.ProcessCompObjectBizService;
import com.sl.core.engine.plugin.ProcessPluginLoaderManager;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/process/comp/object")
@RequiredArgsConstructor
public class ProcessCompObjectController {

    private final ProcessCompObjectBizService compVersionBizService;


    @PostMapping("/save")
    public Boolean save(@RequestBody ProcessCompObjectDO processComp) {
        return compVersionBizService.save(processComp);
    }


    @PostMapping("/save_and_rebuild")
    public Boolean saveAndRebuild(@RequestBody ProcessCompObjectRebuildRequest processComp) {
        return compVersionBizService.saveAndRebuild(processComp);
    }


    @SneakyThrows
    @GetMapping("/download_demo")
    public void downloadDemo(HttpServletResponse response) {
        try {
// 获取文件名
            String filename = StrUtil.concat(true, "demo.zip");

            byte[] buffer = IoUtil.readBytes(URLUtil.getStream(ClassUtil.getResourceURL("square-lake-plugin-step-demo.zip")));

            response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + buffer.length);
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @SneakyThrows
    @GetMapping("/get_file")
    public void getFile(@RequestParam("compCode") String compCode, @RequestParam("compVersion") String compVersion, HttpServletResponse response) {
        try {
// 获取文件名
            String filename = StrUtil.concat(true, compCode, "@", compVersion, "@") + ".zip";
// 获取文件后缀名
            ByteArrayInputStream inputStream = ProcessPluginLoaderManager.INSTANCE.getResourceCode(StpCompositeUtil.getOrgId(),compCode, compVersion);
            if(ObjectUtil.isNull(inputStream)){
                return;
            }
            byte[] buffer = inputStream.readAllBytes();

//            response.reset();
            response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + buffer.length);
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
